package main

import (
	"strings"
	"strconv"
	"crypto/sha256"
	"encoding/hex"
	"time"
	"fmt"
)

var ti int64  = time.Now().UnixNano()

func main()  {

	difficulty :=7
	var nonce int32=0
	for {
		hash:=calculateHash(nonce)
		fmt.Println(hash)
		if isHashValid(hash,difficulty){
			fmt.Println("nonce: ",nonce)
			//nonce++
			//fmt.Println(calculateHash(nonce))
			break
		}
		nonce++
	}
	
}


func isHashValid(hash string, difficulty int) bool {
	prefix := strings.Repeat("0", difficulty)
	return strings.HasPrefix(hash, prefix)
}



func calculateHash(nonce int32) string {
	//使用到了Index、Timestamp、Datas、PrevHash,Nonce字段用于计算当前块的哈希值
	record :=  strconv.FormatInt(123+ti + int64(nonce),10)
	h := sha256.New()
	h.Write([]byte(record))
	hashed := h.Sum(nil)
	return hex.EncodeToString(hashed)
}